package lt.egidijus.aatest.generator;

import static org.junit.Assert.*;

import help.TestUtils;

import java.io.File;
import java.util.ArrayList;

import junit.framework.Assert;

import lt.egidijus.aatest.junit.JUnitFinder;
import lt.egidijus.aatest.model.AAtestClass;
import lt.egidijus.aatest.model.AAtestList;
import lt.egidijus.aatest.utils.Constants;
import lt.egidijus.aatest.utils.FileUtils;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestSuiteGeneratorTest {
	
	private TestSuiteGenerator generator = null;
	private ArrayList<AAtestClass> randomTests = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		generator = new TestSuiteGenerator();
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.projectName);
		JUnitFinder finder = new JUnitFinder();
		ArrayList<ICompilationUnit> cList =  finder.findJUnitsForProject((JavaCore.create(project)));
		AAtestList listtas = new AAtestList();
		listtas.addList(cList);
		IProject project2 = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		ArrayList<ICompilationUnit> cList2 =  finder.findJUnitsForProject((JavaCore.create(project2)));
		listtas.addList(cList2);
		randomTests = new ArrayList<AAtestClass>();
		for (AAtestClass aClass : listtas.getList()){
			if (aClass.getTestRandom() != null)
				randomTests.add(aClass);
		}
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCreateTestSuite() {
		try {
			generator.createTestSuite(randomTests);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	@Test
	public void testCreateTestSuite2() {
		try {
			IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
			IJavaProject project = JavaCore.create(iProject);
			IFolder root = project.getProject().getFolder(Constants.folderName);
			IFolder generatedFolder = root.getFolder("testsuite");
			FileUtils.deleteCoverageFolder();
//			generator.createTestSuite(randomTests);
//			if (!generatedFolder.exists()) {
//				throw new Exception("Nesukurtas testo katalogas");
//			}
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	
	@Test
	public void testCreateTestSuite3() {
		try {
//			IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
//			IJavaProject project = JavaCore.create(iProject);
//			IFolder root = project.getProject().getFolder(Constants.folderName);
//			IFolder generatedFolder = root.getFolder("testsuite");
//			if (generatedFolder.exists()){
//				generatedFolder.delete(true, false, null);
//			}
//			generator.createTestSuite(randomTests);
//			project.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
//			generatedFolder = root.getFolder("testsuite");
//			if (!generatedFolder.exists()) {
//				throw new Exception("Nesukurtas testo katalogas");
//			}
//			File genFile = new File(new File(iProject.getLocationURI()),  "\\src\\testsuite\\TestSuite.java");
//			File resFile = new File(new File(iProject.getLocationURI()),  "\\res\\TestSuite.java");
//			Assert.assertTrue(TestUtils.fileContentsEquals(genFile, resFile));
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}


	

	@Test
	public void testGetGeneratedUnit() {
		try {
//			generator.createTestSuite(randomTests);
//			ICompilationUnit unit = generator.getGeneratedUnit();
//			Assert.assertNotNull(unit);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	


}
